package com.bifrost.util;

import org.apache.shiro.crypto.hash.SimpleHash;

/**
 * Author by yanxiaobo
 * Date on 2018/4/10$.
 */
public class SHA256Util {
    public static final String ALGORITHM_NAME = "SHA-256";
    public static final int DEFAUKT_HASH_ITERATIONS = 1;

    /**
     * 使用SHA256算法加密密码
     * @param source 需要加密的密码
     * @param salt 盐值
     * @param hashIterations hash的次数
     * @return
     */
    public static SimpleHash SHA256Encode(Object source, Object salt, int hashIterations) {
        return new SimpleHash(ALGORITHM_NAME,source,salt,hashIterations);
    }

    public static String SHA256EncodeToHex(Object source) {
       return SHA256Encode(source,null,DEFAUKT_HASH_ITERATIONS).toHex();
    }

    public static String SHA256EncodeToHex(Object source, Object salt, int hashIterations) {
        return SHA256Encode(source,salt,hashIterations).toHex();
    }

    public static String SHA256EncodeToHex(Object source,int hashIterations) {
        return SHA256Encode(source,null,hashIterations).toHex();
    }

    //===============================================================
    public static String SHA256EncodeToBase64(Object source) {
        return SHA256Encode(source,null,DEFAUKT_HASH_ITERATIONS).toBase64();
    }

    public static String SHA256EncodeToBase64(Object source, Object salt, int hashIterations) {
        return SHA256Encode(source,salt,hashIterations).toBase64();
    }

    public static String SHA256EncodeToBase64(Object source,int hashIterations) {
        return SHA256Encode(source,null,hashIterations).toBase64();
    }

}
